package pl.edu.agh.io.sylvia.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import pl.edu.agh.io.sylvia.entity.Group;


public class GroupDAO extends GenericDAO {
	
	public void update(Group group) {
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement("UPDATE GROUPS SET name = ?, last_index = ?, servers_id = ? WHERE id = ?");
			statement.setString(1, group.getName());
			statement.setInt(2, (int) group.getLastIndex());
			statement.setInt(3, group.getServer().getId());
			statement.setInt(4,group.getId());
			statement.executeUpdate();
			statement.close();
			statement = null;
		} catch (SQLException e) {
		} finally {
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) { }
		}
	}
	
	public List<Group> getByMonitorId(int id) {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Group> results= new ArrayList<Group>();
		
		try {
			statement = connection.prepareStatement("SELECT * FROM GROUPS JOIN GROUPS_DETAILS ON GROUPS_DETAILS.GROUPS_ID = GROUPS.ID WHERE GROUPS_DETAILS.MONITORS_ID = ?");
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			ServerDAO serverDAO = new ServerDAO();
			while(resultSet.next()) {
				int groupId = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int lastIndex = resultSet.getInt("last_index");
				int serverId = resultSet.getInt("servers_id");
				results.add(new Group(groupId, name, lastIndex, serverDAO.getById(serverId)));
			}
			return results;
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}
	
	@Override
	public List<Group> getAll() {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Group> results= new ArrayList<Group>();
		
		try {
			statement = connection.prepareStatement("SELECT * FROM GROUPS");
			resultSet = statement.executeQuery();
			
			ServerDAO serverDAO = new ServerDAO();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int lastIndex = resultSet.getInt("last_index");
				int serverId = resultSet.getInt("servers_id");
				results.add(new Group(id, name, lastIndex, serverDAO.getById(serverId)));
			}
			return results;
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}

	@Override
	public Group getById(int id) {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
		try {
			statement = connection.prepareStatement("SELECT * FROM GROUPS WHERE id = ?");
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			ServerDAO serverDAO = new ServerDAO();
			if (resultSet.next()) {
				String name = resultSet.getString("name");
				int lastIndex = resultSet.getInt("last_index");
				int serverId = resultSet.getInt("servers_id");
				return new Group(id, name, lastIndex, serverDAO.getById(serverId));
			} else {
				return null;
			}
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			e.printStackTrace();
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}
	
	
}
